const app = getApp()
const myRequest=require('../../utils/require.js')

// pages/detail/detail.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:'',
    workshop:{},
    userInfo:{},
    workshopRecommend:[],
    slider_count: 1,
    indicatorDots: false,
    current:1,
    user:'',
    mobile:'',
    qrcode:'',
    showDialog: false,
  
    show:false,
    button_disabled:false,
    button_loading:false,
    getMessage:true,
    owner:'',
    mobile:'',
    latitude:'',
    longitude:'',
    markers: [],
    updateList:[],
    is_show:1
  
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    let id = options.id
    let is_show=options.is_show
    if(is_show!=undefined){
      this.setData({
        is_show
      })
    }
    app.CheckSession().then(res => {
      if (res == true) {
        app.GetUser().then(data => {
          this.setData({
            userInfo: data,
            id:id
          })
          this.IsCollect(id)
          this.IsEdit(id)
          myRequest.myRequest('DisSource/detail/' + id, {}, 'GET').then(detail => {
            if (detail.code == "success") {
              this.setData({
                workshop: detail.data,
                slider_count: Object.keys(detail.data.slider).length,
                current: 1
              })

             /* var article = detail.data.desc;
              /**
              * WxParse.wxParse(bindName , type, data, target,imagePadding)
              * 1.bindName绑定的数据名(必填)
              * 2.type可以为html或者md(必填)
              * 3.data为传入的具体数据(必填)
              * 4.target为Page对象,一般为this(必填)
              * 5.imagePadding为当图片自适应是左右的单一padding(默认为0,可选)
             
              var that = this;
              WxParse.wxParse('article', 'html', article, that, 5);
             */
              this.onEditorReady()
              wx.setNavigationBarTitle({
                title: detail.data.title.substring(0, 13) + '...',
              })
              this.UpdateList(id)
              this.CanView(id)
            }else{

            }

          })

        })

      }
    })


 
  },
  mingpian(e){
    myRequest.myRequest('User/agent/', {mobile:this.data.userInfo.mobile}, 'get').then(res => {
      console.log('agent',res)
        if(res.code=='success'){
          wx.navigateToMiniProgram({
            appId: app.globalData.appId,
            path: 'pages/detail/detail?user_id='+res.data.id+'&workshop_id=' + this.data.workshop.workshop_id,
            extraData: {},
            envVersion: '',
            success: function (res) { },
            fail: function (res) { },
            complete: function (res) { },
          })
        }else{
          wx.showToast({
            title: res.message,
            icon: 'none',
            mask: true,
            success: function(res) {},
            fail: function(res) {},
            complete: function(res) {},
          })
        }

    })
 
  },
  onEditorReady() {
    const that = this
    console.log('onEditorReady')
   
    wx.createSelectorQuery().select('#editor').context(res => {
      that.editorCtx = res.context
      that.editorCtx.setContents({
        html: this.data.workshop.desc,
        success: (res) => {
          console.log(res)
        },
        fail: (res) => {
          console.log(res)
        }
      })

      
   /*     myRequest.myRequest('DisSource/workshop/' + this.data.id, {}, 'GET').then(success => {
          that.editorCtx.setContents({
            html: success.data.owndesc,
            success: (res) => {
              console.log(res)
            },
            fail: (res) => {
              console.log(res)
            }
          })
        })*/

    }).exec()


    wx.createSelectorQuery().select('#editor1').context(res => {
      that.editorCtx = res.context
      that.editorCtx.setContents({
        html: this.data.workshop.inside_desc,
        success: (res) => {
          console.log(res)
        },
        fail: (res) => {
          console.log(res)
        }
      })


      /*     myRequest.myRequest('DisSource/workshop/' + this.data.id, {}, 'GET').then(success => {
             that.editorCtx.setContents({
               html: success.data.owndesc,
               success: (res) => {
                 console.log(res)
               },
               fail: (res) => {
                 console.log(res)
               }
             })
           })*/

    }).exec()

  },
  //分享
  onShareAppMessage: function(options) {
    console.log(options)
    console.log(this)
    return {
      title: this.data.workshop.title,
      path: '/pages/detail/detail?id='+this.data.workshop.workshop_id+'&is_show=0',
      success: function (res) {
        console.log('成功', res)
      }
    }
  },
  CanView(id){
    myRequest.myRequest('DisSource/can/view', { workshop_id: id }, 'POST').then(
      res => {
        if (res.code == 'success') {
          let marker = []
          marker.push({ id: 1, name: res.data.address, latitude: res.data.latitude, longitude: res.data.longitude })
          this.setData({
            mobile: res.data.mobile,
            owner: res.data.owner,
            address: res.data.address,
            latitude: res.data.latitude,
            longitude: res.data.longitude,
            getMessage: false,
            markers: marker
          })
        } else {
          this.setData({
            getMessage: true
          })
        }
      }
    )
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

 
  /**
   * 预览图片
   */
  previewImage: function (e) {
    console.log(this.data.workshop)
    var current = e.target.dataset.src;
    wx.previewImage({
      current: current, // 当前显示图片的http链接  
      urls: this.data.workshop.slider // 需要预览的图片http链接列表  
    })
  },
    //地图
  callMap() {
  
    wx.openLocation({//​使用微信内置地图查看位置。
      latitude: parseFloat(this.data.workshop.lat),//要去的纬度-地址
      longitude: parseFloat(this.data.workshop.lng),//要去的经度-地址
      name: this.data.workshop.address,
      address: this.data.workshop.address
    })
  },
  //是否收藏
  IsCollect(id){
    myRequest.myRequest('DisSource/collect/'+id,{},'GET').then(
        res=>{
          if(res.code=='success'){
            this.setData({
              isCollect:true
            })
          }else{
            this.setData({
              isCollect: false
            })
          }
        }
    )
  },
  /**
   * 收藏或取消
   */
  Collect(e){
    myRequest.myRequest('DisSource/collect/'+this.data.workshop.workshop_id,{},'POST').then(res=>{
        if(res.code=="success"){
          if(res.data==1){
              this.setData({
                isCollect: true
              })
          }else{
            this.setData({
              isCollect: false
            })
          }
          wx.showToast({
            title: res.message,
          })
        }
    })
  },
  //更新
  IsEdit(id){
    myRequest.myRequest('DisSource/edit/' + id, {}, 'get').then(res => {
      if (res.code == "success") {

          this.setData({
            isEdit: true
          })
     
      }else{
        this.setData({
          isEdit: false
        })
      }
    })
  },
  //更新详情
  UpdateList(id){
 
    myRequest.myRequest('DisSource/updateList/' + id, {}, 'get').then(res => {
      if (res.code == "success") {

        this.setData({
          updateList: res.data
        })

      } else {
        console.log(res.message)
      }
    })
  },
  //电话
  makeCall: function (e) {
 
    wx.makePhoneCall({
      phoneNumber: this.data.mobile
    })
  },
  //绑定slider
  bindchange:function(e){
      console.log(e)
      this.setData({
        current:e.detail.current+1
      })
  },
  //跳转预约看厂页面
  yuyue:function(e){
    let workshop_id = e.currentTarget.dataset.detail
    if(!this.data.mobile){
      let url = '/pages/yuyue/index?workshop_id='+workshop_id
      wx.navigateTo({
        url: '/pages/mobile/mobile?url=' + encodeURIComponent(url),
      })
      return ;
    }
  
    wx.navigateTo({
      url: '/pages/yuyue/index?workshop_id='+workshop_id,
    })
  },
  getPhoneNumber(e) {
    console.log(e.detail.errMsg)
    console.log(e.detail.iv)
    console.log(e.detail.encryptedData)
    console.log(1111)
    wx.request({
      url: app.globalData.apiurl + 'jiemi',
      method: "GET",
      data: {
        iv: e.detail.iv,
        data: e.detail.encryptedData,
        openid: this.data.user.openid
      },
      success: (res) => {
        if (res.data.code == 0) {
          app.globalData.user = res.data.data
          this.setData({
            user: res.data.data,
            mobile: res.data.data.mobile,
          })    
          wx.navigateTo({
            url: '/pages/yuyue/index?workshop_id=' +this.data.workshop.workshop_id,
          })
        } else {
          console.log('失败')
        }
      }

    })
  },
   //跳转投诉看厂页面
  bindtousu: function (e) {
    let url = '/pages/tousu/tousu'
    if (!this.data.mobile) {
      wx.navigateTo({
        url: '/pages/mobile/mobile?url=' + encodeURIComponent(url),
      })
      return;
    }
    wx.navigateTo({
      url: url,
    })
  },
  //保存图片
  baocun:function(e){
    this.setData({
      button_loading: true,
    })

    wx.downloadFile({
      url:this.data.qrcode,
      success:res=>{
        if(res.statusCode==200){
          let img=res.tempFilePath
          wx.saveImageToPhotosAlbum({
            filePath: img,
            success:res=>{
              this.setData({
                button_loading: false,
              })
              wx.showToast({title:'保存成功'}) 
              this.toggleDialog()
             
            },
            fail:res=>{
              this.setData({
                button_loading: false,
              })
              wx.showToast({ title: '保存失败' }) 
            }
          })
        }
      }
    })
  },
  //弹窗
  toggleDialog() {
    this.setData({
      showDialog: !this.data.showDialog
    });
  },
  messageBtn(){
    myRequest.myRequest('DisSource/apply/'+this.data.workshop.workshop_id,{},'POST').then(res=>{
      if(res.code=='workshop'){
        this.CanView(this.data.workshop.workshop_id)
        return
      }
      if(res.code=='success'){
        wx.showToast({
          title: res.message,
          icon: '',
          image: '',
          mask: true,
          success: function(res) {},
          fail: function(res) {},
          complete: function(res) {},
        })
      }else{
        wx.showToast({
          title: res.message,
          icon: '',
          image: '',
          mask: true,
          success: function (res) { },
          fail: function (res) { },
          complete: function (res) { },
        })
      }
    })
  },
  getLocation: function () {
    wx.getLocation({
      type: 'wgs84',
      success: res => {
        wx.openLocation({//​使用微信内置地图查看位置。
          latitude: this.data.latitude,//要去的纬度-地址
          longitude: this.data.longitude,//要去的经度-地址
          name: this.data.address,
          address: this.data.address
        })
      }
    })},
  goUpdate: function (e) {
    wx.redirectTo({
      url: '/pages/update/update?id='+this.data.workshop.workshop_id,
    })
  },

  Edit(e){
      wx.navigateTo({
        url: '/pages/fabu/index?id=' + this.data.workshop.workshop_id,
        success: function(res) {},
        fail: function(res) {},
        complete: function(res) {},
      })
  }
})